#############################################################################
# FigureA1.R
# Aim: to replicate Figure A1 in Atsusaka and Stevenson (2021)
# Run time: about 0.3 seconds
#############################################################################

rm(list=ls())
start <- Sys.time()
library(viridis)

pal = viridis(10)
pal = pal[4:9]

pdf("FigureA1.pdf", width=10, height=4.5)
par(mfrow=c(1,2), mar=c(5.1, 4.1, 1, 1.8)) # b,l,t,r
pi_vec <- c(0.01, 0.1, 0.2, 0.3, 0.4) # TRUTH PROPORTION


plot(0, type="n", xlim=c(0,100), ylim=c(0,0.5),
      ylab="Bias", xlab="Inattentive Respondens (%)", cex.lab=1.5)
text(10,0.45, labels="p=0.1", cex=1.5)
abline(h=0.5, lty=2, col="dimgray")
abline(h=0.4, lty=2, col="dimgray")
abline(h=0.3, lty=2, col="dimgray")
abline(h=0.2, lty=2, col="dimgray")
abline(h=0.1, lty=2, col="dimgray")


for(i in seq_along(pi_vec)){
 pi = pi_vec[i]   
 gamma <- seq(from=0,to=1,by=0.01) # Proportion of Attentive Respondents
 p <- 0.1  # KNOWN PROPORTION
 lambda <- (pi*p + (1-pi)*(1-p))*gamma + 0.5*(1-gamma) # Kappa=0.2
 
 B = (0.5 - 1/(2*gamma))*( (lambda-0.5)/(p-0.5)   ) # BIAS (THEORETICAL)
 non_gamma <- (1 - gamma)*100        # % of INattentive Respondens
 lines(B ~ non_gamma, lwd=4, col=pal[i])
}
text(95, 0.06, labels=expression(paste(pi, "=0.4")), col=pal[5], cex=1.2, font=2)
text(95, 0.16, labels=expression(paste(pi, "=0.3")), col=pal[4], cex=1.2, font=2)
text(95, 0.25, labels=expression(paste(pi, "=0.2")), col=pal[3], cex=1.2, font=2)
text(95, 0.335, labels=expression(paste(pi, "=0.1")), col=pal[2], cex=1.2, font=2)
text(83, 0.47, labels=expression(paste(pi, "=0.01")), col=pal[1], cex=1.2, font=2)


plot(0, type="n", xlim=c(0,100), ylim=c(0,0.5),
      ylab="Bias", xlab="Inattentive Respondens (%)", cex.lab=1.5)
text(10,0.45, labels="p=0.3", cex=1.5)
abline(h=0.5, lty=2, col="dimgray")
abline(h=0.4, lty=2, col="dimgray")
abline(h=0.3, lty=2, col="dimgray")
abline(h=0.2, lty=2, col="dimgray")
abline(h=0.1, lty=2, col="dimgray")


for(i in seq_along(pi_vec)){
 pi2 = pi_vec[i]   
 gamma2 <- seq(from=0,to=1,by=0.01) # Proportion of Attentive Respondents
 p2 <- 0.3  # KNOWN PROPORTION
 lambda2 <- (pi2*p2 + (1-pi2)*(1-p2))*gamma2 + 0.5*(1-gamma2) # Kappa=0.2
 
 B2 = (0.5 - 1/(2*gamma2))*( (lambda2-0.5)/(p2-0.5)   ) # BIAS (THEORETICAL)
 non_gamma2 <- (1 - gamma2)*100        # % of INattentive Respondens
 lines(B2 ~ non_gamma2, lwd=4, col=pal[i])
}
text(95, 0.06, labels=expression(paste(pi, "=0.4")), col=pal[5], cex=1.2, font=2)
text(95, 0.16, labels=expression(paste(pi, "=0.3")), col=pal[4], cex=1.2, font=2)
text(95, 0.25, labels=expression(paste(pi, "=0.2")), col=pal[3], cex=1.2, font=2)
text(95, 0.335, labels=expression(paste(pi, "=0.1")), col=pal[2], cex=1.2, font=2)
text(83, 0.47, labels=expression(paste(pi, "=0.01")), col=pal[1], cex=1.2, font=2)


dev.off()

end <- Sys.time()
start - end

#############################################################################
# END OF THIS R SOURCE FILE
#############################################################################